import { setTotalCount, getTotalCount } from "@/utils/auth"
import cartApi from "@/api/shopcart"
import { Toast } from 'vant';
export default {
    state: {
        totalCount: getTotalCount(),
        goodsColor: [
            "藏青色",
            "保罗格蓝",
            "伊丽莎白",
            "青青草原青",
            "千鸟格黑白"
        ],
        goodsSize: ["S", "M", "L", "XL", "XXL"]
    },
    mutations: {
        SET_COUNT(state, count) {
            state.totalCount = count
            setTotalCount(count)
        },
    },
    actions: {
        addcart(context, { username, gid, gcount, color, size }) {
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await cartApi.addCart(username, gid, gcount, color, size)
                    context.commit("SET_COUNT", p.data.totalcount)
                    if (p.data.flag) {
                        resolve(p.data)
                    } else {
                        Toast.fail("添加失败")
                    }
                } catch (err) {
                    reject(err)
                }
            })
        },
        getcart(context, username) {
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await cartApi.getCart(username)
                    if (p.data.flag) {
                        context.commit("SET_COUNT", p.data.totalcount)
                        resolve(p.data)
                    }
                } catch (err) {
                    reject(err)
                }
            })
        },
        changecount(context, { username, gid, gcount ,color,size}) {
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await cartApi.changeCount(username, gid, gcount,color,size)
                    if (p.data.flag) {
                        context.commit("SET_COUNT", p.data.totalcount)
                        resolve(p)
                    } else {
                        Toast.fail(p.data.message)
                    }
                } catch (err) {
                    reject(err)
                }
            })
        },
        delgoods(context, { username, gid ,color,size}) {
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await cartApi.delGoods(username, gid,color,size);
                    if (p.data.flag) {
                        context.commit("SET_COUNT", p.data.totalcount)
                        resolve(p)
                    }
                } catch (err) {
                    reject(err)
                }
            })
        },
        delcheckgoods(context, username) {
            return new Promise(async (resolve, reject) => {
                try {
                    let p = await cartApi.delCheckGoods(username)
                    if (p.data.flag) {
                        context.commit("SET_COUNT", p.data.totalcount)
                        resolve(p.data)
                    }
                } catch (err) {
                    reject(err)
                }
            })
        }
    }
}